<template>
  <div id="container"></div>
</template>

<script setup>
import { onMounted, onUnmounted } from "vue";
import { Chart } from "@antv/g2";

let chart;

onMounted(() => {
  chart = new Chart({
    container,
    autoFit: true,
  });

  chart
    .title('限制宽度的柱形图')
    .interval()
    .data([
        { frequency: 0.08167, letter: "A" },
        { frequency: 0.08167, letter: "B" },
        { frequency: 0.08167, letter: "C" },
        { frequency: 0.08167, letter: "D" },
        { frequency: 0.08167, letter: "E" },
        { frequency: 0.08167, letter: "F" },
    ])
    .encode('x', 'letter')
    .encode('y', 'frequency')
    .scale('x', {padding: 0.1})
    .style({
      maxWidth: 120,
      minWidth: 100,
      fill: 'green'
    })

  chart.render();
});

// 销毁
onUnmounted(() => {
  chart.destroy();
  chart = null;
});
</script>

<style lang="less" scoped>
#container {
  width: 800px;
  height: 400px;
}
</style>
